﻿/*
创建人：zhangrj
日期：2022-01-10
描述：派工创建窗体页面js
*/

var page = {
    pagetotal: 0,
    pageSize: 10,
    pageNumber: 1,
    parentID: "", //父窗体ID
    dispatchType: "", //派工类型（1、单条派工，2、批量派工）
    url: getRootPath() + "/Ashx/YePianContainer/YPContainerDispatchFormHandler.ashx", //后台处理逻辑url
    postFormData: { //保存提交时的数据
        action: "SaveDispatchData"
    },
    init: function () {
        page.bind();
        page.initPageData();
    },
    //初始化页面事件
    bind: function () {
        //批次列表
        $("#gdContainerTable").datagrid({
            columns: [[
		         { field: 'CONTAINERNAME', title: '批次号', width: 180 },
                 { field: 'PRODUCTNAME', title: '零件号', width: 128 },
                 { field: 'PRODUCTDESCRIPTION', title: '零件名称', width: 180 },
                 { field: 'PLANQTY', title: '计划数', width: 80 },
                 { field: 'WIPQTY', title: '在制数', width: 80 },
                 { field: 'WORKFLOWSTEPNAME', title: '当前工序', width: 180 },
                 { field: 'CONTAINERTYPENAME', title: '批次类型', width: 140 },
                 { field: 'WORKFLOWSTEPID', title: '', width: 180 },
                 { field: 'CONTAINERTYPE', title: '', width: 140, hidden: true },
                 { field: 'CONTAINERID', title: '', width: 200, hidden: true },
                 { field: 'WORKFLOWID', title: '', width: 200, hidden: true },
                 { field: 'SEQUENCE', title: '', width: 200, hidden: true }
                ]]
        });
        //工序列表
        $("#gdStepTable").datagrid({
            columns: [[
               { field: 'ck', title: '选择', checkbox: true, width: 30 },
		         { field: 'WORKFLOWSTEPNAME', title: '工序', width: 190 },
                 { field: 'CanDispatchQty', title: '可派数', width: 120 },
                   { field: 'SONWFSTEP', title: '子工艺', width: 110 },
                  { field: 'WORKFLOWSTEPID', title: '', width: 200, hidden: true }
                ]],
            onClickRow: function (rowIndex, rowData) {
            }
        });
        //零件序号
        $("#gdProductTable").datagrid({
            columns: [[
		         { field: 'CONTAINERNAME', title: '零件序号', width: 166 },
                { field: 'CONTAINERID', title: '', width: 200, hidden: true }
                ]]
        });
        //派工信息
        $("#gdDispatchTable").datagrid({
            columns: [[
		        { field: 'CONTAINERNAME', title: '批次号', width: 150 },
                 { field: 'PRODUCTNAME', title: '工序', width: 158 },
                 { field: 'QTY', title: '派工数', width: 80 },
                 { field: 'WORKFLOWNAME', title: '报工数', width: 80 }
                ]],
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
                //页码跳转事件
                $('#pageGo').on('click', function () {
                    page.pageNumber = $(this).parents('tr').find('.pagination-num').val();
                    page.bindDispatchTableData();
                });
            }
        });
        //显示派工复选框事件
        $('#ckDispatch').on("click", function () {
            clearMessage(); //清空提示消息
            var isCheck = $(this).prop("checked");
            if (isCheck) { //选中
                page.bindDispatchTableData();
            } else {
                clearEasyuiDataGrid("gdDispatchTable", 1); //清空列表
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.bindDispatchTableData();
            }
        });
        //派工
        $('#btnStart').on("click", function () {
            page.postData("1");
        });
        //派工并关闭
        $('#btnStartAndClosed').on("click", function () {
            page.postData("2");
        });
        //关闭
        $('#btnClose').on("click", function () {
            parent.window.returnValue = { page: "YPContainerDispatchFormPage.html", isSuccess: true, message: "已关闭窗体" };
            parent.$("#" + page.parentID).dialog("close");
        })
    },
    //初始化页面数据
    initPageData: function () {
        page.parentID = getParam("parentDiv");
        page.dispatchType = getParam("dispatchType");
        if (page.dispatchType == "2") {
            $("#divQty").hide(); //如果是批量派工，隐藏数量输入框
        }
        var containers = parent.$("#" + page.parentID).dialog('options').poPupData;
        //批次列表
        $("#gdContainerTable").datagrid('loadData', { rows: containers });
        $.ajax({
            type: "Post",
            url: page.url,
            data: { action: "GetInitData", containers: JSON.stringify(containers) },
            cashe: false,
            success: function (data) {
                var result = JSON.parse(data);
                if (result.Result) {
                    page.bindDefaultPageData(result.DBDataSet);
                } else {
                    displayMessage(result.Message, false);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    },
    //绑定初始化后台获取的数据
    bindDefaultPageData: function (resData) {
        //工序列表
        $("#gdStepTable").datagrid('loadData', { rows: resData.dtWorkStep });
        //子工序列表
        $("#gdProductTable").datagrid('loadData', { rows: resData.childDt });
        //优先级
        $("#selPriorityCode").combobox({
            valueField: 'PRIORITYCODE',
            textField: 'PRIORITYCODENAME',
            data: resData.natdt, //下拉数据
            value: resData.natdt[0].PRIORITYCODE//默认选中第一个
        });
        //班组数据
        $("#selTeam").combobox({
            valueField: 'TEAMID',
            textField: 'TEAMNAME',
            data: resData.dtTeam, //下拉数据
            onSelect: function (record) { //下拉时动态获取操作工
                $("#selResource").combobox("clear");
                $.ajax({
                    type: "Post",
                    url: page.url,
                    data: { action: "GetOperator", teamID: record.TEAMID },
                    cashe: false,
                    success: function (data) {
                        var result = JSON.parse(data);
                        $('#selOperator').combogrid({
                            multiple: true, //设置允许多选
                            panelWidth: 120,
                            panelHeight: 120,
                            idField: 'EMPLOYEEID',
                            textField: 'FULLNAME',
                            checkOnSelect: true,
                            columns: [[
                                { field: 'ck', title: '', width: 60, checkbox: true },
                                { field: 'EMPLOYEEID', title: '', width: 60, hidden: true },
                                { field: 'FULLNAME', title: '姓名', width: 110 }
                            ]],
                            data: result.DBTable
                        });
                        //操作工选择页面
                        $('#selOperator').combogrid('grid').datagrid({
                            onClickRow: function (rowIndex, rowData) {
                                var rows = $('#selOperator').combogrid('grid').datagrid('getChecked');
                                var stremployeeid = "";
                                for (var i = 0; i < rows.length; i++) {
                                    if (i == 0) {
                                        stremployeeid += "em.employeeid='" + rows[i].EMPLOYEEID + "'";
                                    } else {
                                        stremployeeid += " or em.employeeid='" + rows[i].EMPLOYEEID + "'";
                                    }
                                }
                                $.ajax({
                                    type: "Post",
                                    url: page.url,
                                    data: { action: "GetResource", employeeids: stremployeeid },
                                    cashe: false,
                                    success: function (data) {
                                        var result = JSON.parse(data);
                                        $("#selResource").combobox({ //绑定设备
                                            valueField: 'RESOURCEID',
                                            textField: 'RESNAME',
                                            data: result.DBTable //下拉数据
                                        });
                                    },
                                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                                        displayMessage(XMLHttpRequest.responseText, false);
                                    }
                                });
                            }
                        });
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        displayMessage(XMLHttpRequest.responseText, false);
                    }
                });
            }
        });
    },
    //绑定操作工派工信息
    bindDispatchTableData: function () {
        var employeeid = "";
        var rows = $('#selOperator').combogrid('grid').datagrid('getChecked');
        if (rows.length > 0) {
            employeeid = rows[0].EMPLOYEEID;
        } else {
            displayMessage("未选择操作工", false);
            return;
        }
        var disPostData = {
            action: "GetWorkerDispatchAndReportInfo",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            employeeid: employeeid
        };
        $.ajax({
            type: "Post",
            url: page.url,
            data: disPostData,
            cashe: false,
            success: function (data) {
                var result = JSON.parse(data);
                page.pagetotal = result.RowCount;
                $("#gdDispatchTable").datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    },
    //提交数据
    postData: function (btype) {
        clearMessage(); //清空提示消息
        if (isNullOrWhiteSpaceStr(page.dispatchType)) {
            displayMessage("数据过期，请刷新后再试", false);
            return;
        }
        $("#btnStart").attr("disabled", "disabled"); //禁用按钮
        $("#btnStartAndClosed").attr("disabled", "disabled"); //禁用按钮
        var stepRows = $("#gdStepTable").datagrid('getChecked');
        if (stepRows.length == 0) {
            displayMessage2("请选择工序", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        page.postFormData.teamId = $("#selTeam").combobox('getValue'); //班组ID
        if (isNullOrWhiteSpaceStr(page.postFormData.teamId)) {
            displayMessage2("请选择班组", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        var operaRows = $('#selOperator').combogrid('grid').datagrid('getChecked'); //操作工  
        if (operaRows.length == 0) {
            displayMessage2("请选择操作工", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        if (page.dispatchType == "1") {
            page.postFormData.dispatchQty = $("#txtQty").textbox('getValue'); //数量
            if (isNullOrWhiteSpaceStr(page.postFormData.dispatchQty)) {
                displayMessage2("请填写数量", false, ["btnStart", "btnStartAndClosed"]);
                return;
            }
        }
        page.postFormData.priorityCode = $("#selPriorityCode").combobox('getValue'); //优先级
        page.postFormData.resource = $("#selResource").combobox('getValue'); //设备ID
//        if (isNullOrWhiteSpaceStr(page.postFormData.resource)) {
//            displayMessage2("请选择设备", false, ["btnStart", "btnStartAndClosed"]);
//            return;
//        }

        page.postFormData.startDate = $("#txtStartDate").datebox('getValue'); //开始日期
        page.postFormData.endDate = $("#txtEndDate").datebox('getValue'); //结束日期

        if (isNullOrWhiteSpaceStr(page.postFormData.startDate) || isNullOrWhiteSpaceStr(page.postFormData.endDate)) {
            displayMessage2("请选择开始日期或结束日期", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        //批次列表
        var dtContainers = $("#gdContainerTable").datagrid('getRows');
        if (dtContainers.length == 0) {
            displayMessage2("未有批次信息", false, ["btnStart", "btnStartAndClosed"]);
            return;
        }
        page.postFormData.dtContainers = JSON.stringify(dtContainers);
        page.postFormData.dispatchType = page.dispatchType;
        page.postFormData.stepDatas = JSON.stringify(stepRows); //工序
        page.postFormData.operatorDatas = JSON.stringify(operaRows); //操作工  

        $.ajax({
            type: "Post",
            url: page.url,
            data: page.postFormData,
            cashe: false,
            success: function (data) {
                var result = JSON.parse(data);
                if (result.Result) {
                    if (btype == "2") {
                        parent.window.returnValue = { page: "YPContainerDispatchFormPage.html", isSuccess: true, message: result.Message };
                        parent.$("#" + page.parentID).dialog("close");
                    }
                }
                displayMessage2(result.Message, result.Result, ["btnStart", "btnStartAndClosed"]);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                displayMessage2(XMLHttpRequest.responseText, false, ["btnStart", "btnStartAndClosed"]);
            }
        });
    }
};
page.init();